cd '/.../structural-model-code';
addpath ('...data/HRS MiCDA/Tables');
addpath ('...data/FSRDC/Tables');
addpath ('...data');
addpath ('.../structural-model-code/lininterp');



        %=========================================================================%
        % parameters
        %=========================================================================%
        aT    = 81;            %terminal age
        aS    = 50;            %start age
        r     = .029;          %interest rate
        pi    = .028;          %inflation
        %1. SSA mortality rates by age (48+)
        pdeath = csvread('SSA_mortality_2010.csv',1,0);
        pdeath = pdeath(aS+1:end,:);
        avpdeath = mean(pdeath(:,2:3),2);
        mortp = avpdeath(1:aT-aS);
        mortp_80 = mortp./avpdeath(aT-aS+1);
        
        %2. annuity factors
        afactor = zeros(length(mortp_80),1); 
        %loop over age
        for a = 1:length(afactor)
            disc = zeros(length(afactor)-a+1,1);
            disc(1) = 1;
            for i = 2:length(disc)
                disc(i) = (1-mortp_80(a+i-1))*(1/((1+r)*(1+pi)))^(i-1); 
            end

            afactor(a) = sum(disc);
        end 
        ages = (aS:1:aT-1)';
        
        afactor = [pdeath(1:aT-aS,1) afactor(1:aT-aS)];
        %%
        csvwrite('.../data/sim_annfactor.csv',afactor);

